package com.skynet.springboot.mybatis.springmybatis.controller;

import com.skynet.springboot.mybatis.springmybatis.annotation.Log;
import com.skynet.springboot.mybatis.springmybatis.dto.ResultBean;
import com.skynet.springboot.mybatis.springmybatis.entity.Employee;
import com.skynet.springboot.mybatis.springmybatis.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Skynet
 * @Date 2017/9/8-0:06.
 */
@RequestMapping("/employees")
@RestController
@Api("接口测试")
public class EmployeeController {

    private static final String SUCCESS = "success";

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "获取员工列表", notes = "获取员工列表")
    @GetMapping("/")
    public ResultBean<List<Employee>> getEmployeeList() {
        return new ResultBean<>(employeeService.queryAllEmployee());
    }

    @ApiOperation(value = "根据ID获取员工", notes = "根据ID获取员工")
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id}")
    public ResultBean<Employee> getEmployee(@PathVariable("id") Integer id) {
        return new ResultBean<>(employeeService.queryEmployeeById(id));
    }

    @ApiOperation(value = "新增员工", notes = "新增员工")
    @ApiImplicitParam(name = "employee", value = "员工实体", required = true, dataType = "Employee")
    @PostMapping("/")
    public ResultBean<String> saveEmployee(@RequestBody Employee employee) {
        employeeService.saveEmployee(employee);
        return new ResultBean<>();
    }

    @Log("更新员工信息")
    @ApiOperation(value = "更新员工信息", notes = "更新员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "employee", value = "员工", required = true, dataType = "employee")
    })
    @PutMapping("/{id}")
    public ResultBean<String> updateEmployee(@PathVariable Integer id, @RequestBody Employee employee) {
        Employee emp = employeeService.queryEmployeeById(id);
        emp.setName(employee.getName());
        emp.setEmail(employee.getEmail());
        emp.setGender(employee.getGender());
        employeeService.updateEmployee(emp);
        return new ResultBean<>();
    }

    @Log("删除员工信息")
    @ApiOperation(value = "根据ID删除员工", notes = "根据ID删除员工")
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultBean<String> deleteEmployeeById(@PathVariable("id") Integer id) {
        employeeService.deleteEmployeeById(id);
        return new ResultBean<>();
    }


}
